package com.qyhl.webtv.module_live.utils.ItemViewDelegete;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomViewTarget;
import com.bumptech.glide.request.transition.Transition;
import com.qyhl.webtv.basiclib.utils.DateUtils;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.live.TeleTextLiveBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.commonlib.utils.view.expandableView.text.ExpandableTextView;
import com.qyhl.webtv.commonlib.utils.view.radiobutton.MediaManager;
import com.qyhl.webtv.commonlib.utils.view.video.emptyvideo.EmptyControlVideoActivity;
import com.qyhl.webtv.module_live.R;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;


/**
 * Created by qyhl on 2017/8/14.
 */

public class VideoItemDelegete implements ItemViewDelegate<TeleTextLiveBean> {
    private List<TeleTextLiveBean> liveBean;
    private Context context;


    public VideoItemDelegete(Context context, List<TeleTextLiveBean> liveBeen) {
        this.context = context;
        this.liveBean = liveBeen;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.live_item_teletext_live_video;
    }

    @Override
    public boolean isForViewType(TeleTextLiveBean item, int position) {
        if ("1".equalsIgnoreCase(item.getType())) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void convert(ViewHolder holder, TeleTextLiveBean teleTextLiveBean, int position) {
        //设置时间轴
        TextView time = holder.getView(R.id.item_time);
        time.setText(DateUtils.teleTextDateFormat(teleTextLiveBean.getPublishtime()));//时间

        //设置作者
        TextView author = holder.getView(R.id.item_author);
        if (StringUtils.isEmpty(teleTextLiveBean.getAuthor())) {
            author.setVisibility(View.GONE);
        } else {
            author.setVisibility(View.VISIBLE);
            author.setText(StringUtils.encodePhoneNum(teleTextLiveBean.getAuthor()));
        }

        //设置简介
        ExpandableTextView content = holder.getView(R.id.item_content);
        if (StringUtils.isEmpty(teleTextLiveBean.getContent())) {
            content.setVisibility(View.GONE);
        } else {
            content.setVisibility(View.VISIBLE);
            content.setText(teleTextLiveBean.getContent(), position);
        }

        //设置日期
        TextView date = holder.getView(R.id.item_date);
        LinearLayout dateLayout = holder.getView(R.id.top_layout);
        if (position != 0) {
            if (liveBean.size() > 0) {
                if (DateUtils.isSameDay(teleTextLiveBean.getPublishtime(), liveBean.get(position - 1).getPublishtime())) {
                    dateLayout.setVisibility(View.GONE);
                } else {
                    dateLayout.setVisibility(View.VISIBLE);
                    date.setText(DateUtils.teleTextFormat(teleTextLiveBean.getPublishtime()));
                }
            } else {
                dateLayout.setVisibility(View.GONE);
            }
        } else {
            dateLayout.setVisibility(View.VISIBLE);
            date.setText(DateUtils.teleTextFormat(teleTextLiveBean.getPublishtime()));
        }

        final ImageView videoCover = holder.getView(R.id.video_cover);
        RequestBuilder<Bitmap> requestBuilder = Glide.with(context).asBitmap();
        requestBuilder.load(teleTextLiveBean.getVideoCover()).apply(new RequestOptions().error(R.drawable.video_list_portrait_default)).into(new CustomViewTarget<ImageView, Bitmap>(videoCover) {
            @Override
            protected void onResourceCleared(@Nullable Drawable placeholder) {

            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {

            }

            @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                int imageWidth = resource.getWidth();
                int imageHeight = resource.getHeight();
                ViewGroup.LayoutParams params = videoCover.getLayoutParams();
                //竖图
                if (imageHeight > imageWidth) {
                    params.height = 600;
                    params.width = 600 * 9 / 16;
                } else {
                    params.width = 600;
                    params.height = 600 * 9 / 16;
                }
                videoCover.setLayoutParams(params);
                videoCover.setImageBitmap(resource);
            }
        });

        RelativeLayout videoLayout = holder.getView(R.id.video_layout);
        videoLayout.setTag(teleTextLiveBean);
        videoLayout.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v) {
                TeleTextLiveBean telBean = (TeleTextLiveBean) v.getTag();
                MediaManager me = MediaManager.getIntance();
                if (me.isPlay()) {
                    me.release();
                    me.getTextView().endAnimation();
                    //恢复时间
                    me.getTextView().setContent(me.getContentBean().getBrokeNews().getAudioDuration());
                }
                Bundle bundle = new Bundle();
                bundle.putString(EmptyControlVideoActivity.IntentKey_VideoUrl, telBean.getVideoUrl());
                bundle.putString(EmptyControlVideoActivity.IntentKey_VideoCover, telBean.getVideoCover());
                RouterManager.router(ARouterPathConstant.VIDEO_EMPTY_CONTROL, bundle);
            }
        });

    }
}
